{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Streaming app from stocks"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "from datetime import datetime\n",
    "from pixiedust.display.app import *\n",
    "from pixiedust.display.streaming import *\n",
    "\n",
    "class StockStreamingAdapter(StreamingDataAdapter):\n",
    "    def __init__(self):\n",
    "        url = 'https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY&symbol=MSFT&interval=1min&apikey=demo'\n",
    "        self.payload = requests.get(url).json()\n",
    "        self.timeSeries = self.payload['Time Series (1min)']\n",
    "        self.metadata = self.payload['Meta Data']\n",
    "        self.allX = [datetime.strptime(dt, '%Y-%m-%d %H:%M:%S').strftime(\"%H:%M\") for dt in self.timeSeries]\n",
    "        self.allY = [w['1. open'] for v,w in iteritems(self.timeSeries)]\n",
    "        self.ptr = None\n",
    "    \n",
    "    def doGetNextData(self):\n",
    "        if self.ptr is None:\n",
    "            self.ptr = 9\n",
    "            return self.allY[0:10]\n",
    "        self.ptr = self.ptr + 1\n",
    "        return self.allY[self.ptr:self.ptr+1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from pixiedust.display.streaming.bokeh import *\n",
    "@PixieApp\n",
    "class StockStreamingApp():    \n",
    "    def setup(self):\n",
    "        self.streamingData = StockStreamingAdapter()\n",
    "        self.streamingDisplay = None\n",
    "\n",
    "    def newDisplayHandler(self, options, entity):\n",
    "        if self.streamingDisplay is None:\n",
    "            self.streamingDisplay = LineChartStreamingDisplay(options, entity)\n",
    "            self.streamingDisplay.windowSize = 20\n",
    "        return self.streamingDisplay\n",
    "        \n",
    "    @route()\n",
    "    def main(self):\n",
    "        return\"\"\"\n",
    "            <div pd_entity=\"streamingData\" pd_refresh_rate=\"1000\">\n",
    "            </div>\n",
    "        \"\"\"\n",
    "\n",
    "#run the app\n",
    "stockApp = StockStreamingApp()\n",
    "stockApp.run(runInDialog='false')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "celltoolbar": "Edit Metadata",
  "kernelspec": {
   "display_name": "pySpark (Spark 1.6.0) Python 2",
   "language": "python",
   "name": "pyspark1.6python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
